<script>
	var url;
	$(document).ready(function(){
		
		newData = function (){
			$('#dialog-nilai').dialog({
				title: 'Input Nilai Siswa',
				style:{background:'#d4d4d4'},
				width: $(window).width() * 0.8,
				height: $(window).height() * 0.99,
				closed: true,
				cache: false,
				href: base_url+'nilai/add2/',
				modal: true
			});
			 
			$('#dialog-nilai').dialog('open');
			url = base_url+'nilai/save';
		}
		// end newData

		importDialog = function (){  
			$('#dlgimport_nilai').dialog('open').dialog('setTitle','Import Data Nilai');
			$('#fmimport_nilai').form('clear');  
			url = base_url+'nilai/import'; 
		}
		
		importData=function(){
			$('#fmimport_nilai').form('submit',{
				url: url,
				onSubmit: function(){
					//return $(this).form('validate');
				},
				success: function(result){
					alert(result);
					var result = eval('('+result+')');
					if (result.success){
						 $.messager.show({
							title: 'Sukses',
							msg: result.msg
						}); 
						$('#dlgimport_nilai').dialog('close');		// close the dialog
						$('#dg').datagrid('reload');	// reload the user data
					} else {
						$.messager.show({
							title: 'Error',
							msg: result.msg
						});
					}
				}
			});
		}
		
		editData = function (){
			$('#dialog-nilai').dialog({
				title: 'Edit Nilai Siswa',
				width: $(window).width() * 0.8,
				height: $(window).height() * 0.99,
				closed: true,
				cache: false,
				href: base_url+'nilai/edit/',
				modal: true
			});
			 
			$('#dialog-nilai').dialog('open');
			url = base_url+'nilai/save';
		}
		
		// load grid
		$(function(){
			$('#dg').datagrid({url:"<?=base_url()?>nilai/grid"});	
			//$('#dg').datagrid('enableFilter'); 			
		});	
		
		// onchange
		$('#s_kd_fakultas').change(function(){
			$('#s_kd_prodi').load(base_url+'prosedur/getProdibyFakultas/'+$('#s_kd_fakultas').val());
		});
		
		$('#s_kd_prodi').change(function(){
			$('#s_kd_kurikulum').load(base_url+'prosedur/getKurikulumbyProdi/'+$('#s_kd_prodi').val());
		});
		
		$('#s_kd_kurikulum').change(function(){
			$('#s_kd_matakuliah').load(base_url+'prosedur/getMatakuliah/'+$('#s_kd_kurikulum').val());
		});
		
		$('#s_kd_matakuliah').change(function(){
			$('#s_kd_dosen').load(base_url+'prosedur/getDosenbyProdiNilai/'+$('#s_kd_matakuliah').val());
		});
		
		$('#s_kd_dosen').change(function(){
			$('#s_kd_kelas').load(base_url+'prosedur/getKelasbyDosenNilai/'+$('#s_kd_dosen').val());
		});
		
		// filter
		filter = function(){
			$('#dg').datagrid('load',{
				kd_prodi : $('#s_kd_prodi').val(),
				kd_matakuliah : $('#s_kd_matakuliah').val(),
				kd_dosen : $('#s_kd_dosen').val(),
				kd_kelas : $('#s_kd_kelas').val(),
			});
			//$('#dg').datagrid('enableFilter');
		}
		
		//# Tombol Bawah
		$(function(){
			var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid
			pager.pagination({
				buttons:[
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>39, 'policy'=>'ADD'))){?>
					{
						iconCls:'icon-add',
						text:'Tambah Data',
						handler:function(){
							newData();
						}
					},
				<?}?>
				<?if($this->mdl_auth->CekAkses(array('menu_id'=>39, 'policy'=>'EDIT'))){?>
					{
						iconCls:'icon-edit',
						text:'Edit Nilai',
						handler:function(){
							editData();
						}
					},
				<?}?>
					{
						iconCls:'icon-upload',
						text:'Import EXCEL',
						handler:function(){
							importDialog();
							//window.open('<?=base_url().'jenjang/import'?>');
						}
					}		
				]
			});			
		});
		
	});
</script>

<table id="dg" title="Daftar Nilai Mahasiswa" data-options="
			rownumbers:true,
			singleSelect:true,
			autoRowHeight:false,
			pagination:true,
			pageSize:30,
			fit:true,
			toolbar:'#toolbar',
			">
	<thead>
		<tr>
			<!--<th field="tahun_penerimaan" sortable="true" width="80">Tahun Penerimaan</th> -->
			<th field="id_krs_detail" hidden="true" sortable="false" width="40">ID </th>
			<th field="nim" sortable="true" width="100">NIM</th>
			<th field="nama" sortable="true" width="250">Nama Mahasiswa</th>
			<th field="nilai_tugas" sortable="true" width="100">Nilai Tugas</th>
			<th field="nilai_uts" sortable="true" width="100">Nilai UTS</th>
			<th field="nilai_uas" sortable="true" width="100">Nilai UAS</th>
			<th field="nilai_quis" sortable="true" width="100">Nilai Quis</th>
			<th field="presensi" sortable="true" width="100">Presensi</th>
			<th field="nilai_final" sortable="true" width="100">Nilai Final</th>
			<th field="nilai_huruf" sortable="true" width="100">Grade</th>			
		</tr>
	</thead>
</table>
<div id="toolbar" style="padding:5px;height:auto">
	<div>
		<table>
			<tr>
			<td>Fakultas</td>
			<td>: 
				<select id="s_kd_fakultas" name="s_kd_fakultas" style="width:200px;">
					<?=$this->mdl_prosedur->OptionFakultas();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Kurikulum</td>
			<td>: 
				<select id="s_kd_kurikulum" name="s_kd_kurikulum" style="width:200px;">
					<?=$this->mdl_prosedur->OptionKurikulum();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Dosen</td>
			<td>: 
				<select id="s_kd_dosen" name="s_kd_dosen" style="width:200px;">
					<?=$this->mdl_prosedur->OptionDosenNilai();?>
				</select>
			</td>
			</tr>
			<tr>
			<td>Prodi</td>
			<td>: 
				<select id="s_kd_prodi" name="s_kd_prodi" style="width:200px;">
					<?=$this->mdl_prosedur->OptionProdi();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Matakuliah</td>
			<td>: 
				<select id="s_kd_matakuliah" name="s_kd_matakuliah" style="width:200px;">
					<?=$this->mdl_prosedur->OptionMatakuliah();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>Kelas</td>
			<td>: 
				<select id="s_kd_kelas" name="s_kd_kelas" style="width:200px;">
					<?=$this->mdl_prosedur->OptionKelasNilai();?>
				</select>
			</td>
			<td>&nbsp;</td>
			<td>&nbsp;&nbsp;<a href="#" onclick="filter()" class="easyui-linkbutton" iconCls="icon-search">Filter</a></td>
			<td>&nbsp;</td>
			</tr>
		</table>
	</div>
</div>
<div id="dialog-nilai" class="easyui-dialog" style="width:400px;height:150px" closed="true" buttons="#dlg-buttons-sap">
		
	</div>
	<div id="dlg-buttons-sap">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="save_nilai()">Save</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dialog-nilai').dialog('close')">Cancel</a>
	</div>


<!-- FORM IMPORT -->
<div id="dlgimport_nilai" class="easyui-dialog" style="width:500px;height:200px;padding:10px 20px" closed="true" buttons="#dlg-buttons">
	<div class="ftitle">Import Data Nilai dari Excel</div>
	<?php 
		$attributes = array('id' => 'fmimport_nilai');
		echo form_open_multipart('', $attributes);
	?>
	
		<div class="fitem">
			<label style="width:100px;vertical-align:top">File</label>
			<input type="file" name="datafile"/>
		</div>
	</form>
	<div id="dlg-buttons">
		<a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="importData()">Upload</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlgimport_nilai').dialog('close')">Cancel</a>
	</div>
</div>